<template>
  <view>
    <u-navbar title="添加车牌" title-size="38" :border-bottom="false">
    </u-navbar>

    <view class="title">请输入车牌号</view>
    <view class="content">请绑定真实有效的车牌号，否则将 无法正常使用功能</view>

    <view class="car-input">
      <view @click="chooseCar">
        <u-message-input
          :maxlength="7"
          :font-size="30"
          :width="60"
          :value="carCard"
          :disabled-keyboard="true"
        >
        </u-message-input>
      </view>
      <view>
        <view
          class="car-input-item new"
          @click="checkNew()"
          :class="isNew == 1 ? 'checkNew' : ''"
        >
          <image class="plus" src="../../static/img/car-plus-greey.svg"></image>
          <view class="name">新能源</view>
        </view>
      </view>
    </view>

    <view class="phone-box" v-for="(item, index) in phones">
      <view class="phone-box-title"> 手机号： </view>
      <input
        class="phone-box-input"
        v-model="phones[index]"
        type="number"
        placeholder="输入车辆司机手机号"
      />
      <image
        class="phone-box-add"
        @click="addPhone"
        src="../../static/img/car-plus.svg"
        v-if="index == phones.length - 1"
      ></image>
      <image
        class="phone-box-close"
        @click="delPhone(index)"
        src="../../static/img/close.svg"
        v-else="index == phones.length - 1"
      ></image>
    </view>

    <view class="car-btn btn" @click="send()">确定</view>

    <u-keyboard
      mode="car"
      v-model="isCar"
      tips="选择车牌"
      :mask="false"
      @change="carChange"
      @backspace="backspace"
    >
    </u-keyboard>
    <u-toast ref="uToast" />
  </view>
</template>

<script>
import { createCar } from "@/api/car.js";
export default {
  data() {
    return {
      isCar: false,
      carCard: "",
      phones: [""],
      isNew: 0,
    };
  },
  methods: {
    chooseCar() {
      this.isCar = !this.isCar;
    },
    carChange(val) {
      this.carCard = this.carCard + val;
    },
    // 退格键被点击
    backspace() {
      // 删除value的最后一个字符
      if (this.carCard.length)
        this.carCard = this.carCard.substr(0, this.carCard.length - 1);
    },
    addPhone() {
      this.phones.push("");
      console.log(this.phones);
    },
    checkNew() {
      this.isNew = this.isNew == 1 ? 0 : 1;
    },

    delPhone(index) {
      this.phones.splice(index, 1);
    },
    send() {
      if (this.carCard.length == 0) {
        this.$refs.uToast.show({
          title: "请输入车牌号!",
          type: "error",
          position: "bottom",
        });
        return;
      }
      // 验证车牌号
      if (
        !this.carCard.match(
          /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/
        )
      ) {
        this.$refs.uToast.show({
          title: "请输入正确的车牌号!",
          type: "error",
          position: "bottom",
        });
        return;
      }

      if (this.phones[0].length == 0) {
        this.$refs.uToast.show({
          title: "请输入手机号!",
          type: "error",
          position: "bottom",
        });
        return;
      }
      // 循环验证手机号
      for (let i = 0; i < this.phones.length; i++) {
        if (
          !this.phones[i].match(
            /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/
          )
        ) {
          this.$refs.uToast.show({
            title: "请输入正确的手机号!",
            type: "error",
            position: "bottom",
          });
          return;
        }
      }

      //  发送请求
      let data = {
        plate_num: this.carCard,
        is_new_energy: this.isNew,
        phones: this.phones,
      };
      createCar(data).then((res) => {
        if (res.data.code == 200) {
          this.$refs.uToast.show({
            title: "添加成功!",
            type: "success",
            position: "bottom",
          });
          setTimeout(() => {
            uni.navigateBack();
          }, 1000);
        }else{
          this.$refs.uToast.show({
            title: res.data.msg,
            type: "error",
            position: "bottom",
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.title {
  font-size: 66rpx;
  font-weight: bold;
  text-align: center;
  margin-top: 60rpx;
}

.content {
  width: 420rpx;
  margin: 20rpx auto;
  text-align: center;
  font-size: 28rpx;
  color: #666666;
}

.car-input {
  border: 4rpx solid #1a88ff;
  margin: 60rpx;
  padding: 30rpx 20rpx;
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;

  &-item {
    width: 60rpx;
    height: 78rpx;
    position: relative;
    z-index: 9999999;
    margin: 6rpx;
    font-size: 30rpx;
    font-weight: bold;
    color: #303133;
    line-height: 45px;
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
    -webkit-flex-direction: row;
    flex-direction: row;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    justify-content: center;
    // flex-direction: column;
    -webkit-box-align: center;
    -webkit-align-items: center;
    align-items: center;
    border: 1px solid #606266;
    border-radius: 6rpx;
  }
}

.checkNew {
  background-color: #1a88ff;
  color: #ffffff;
}

.new {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  position: relative;
  z-index: 9999999;
  color: #b8b8b8;

  .plus {
    width: 24rpx;
    height: 24rpx;
    margin-bottom: 14rpx;
  }

  .name {
    font-size: 20rpx;
    position: absolute;
    width: 100rpx;
    text-align: center;
    bottom: -26rpx;
    transform: scale(0.7);
  }
}

.phone-box {
  margin: 20rpx 60rpx 0;
  border-radius: 16rpx;
  border: 4rpx solid #1a88ff;
  display: flex;
  align-items: center;
  padding: 30rpx 20rpx;
  justify-content: space-between;

  &-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333333;
  }

  &-input {
    flex: 1;
    font-size: 32rpx;
    color: #333333;
  }

  &-add {
    width: 42rpx;
    height: 42rpx;
  }

  &-close {
    width: 42rpx;
    height: 42rpx;
    transform: scale(1.3);
  }
}

.car-btn {
  width: 480rpx;
  height: 88rpx;
  background: linear-gradient(90deg, #1a88ff 0%, #2c6bfc 100%);
  box-shadow: 0px 6px 18px rgba(26, 136, 255, 0.3);
  border-radius: 42px;
  color: #ffffff;
  font-size: 36rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 120rpx auto 0;
}

.car-false {
  width: 480rpx;
  color: #666666;
  font-size: 36rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 40rpx auto 0;
}
</style>
